/*
 * Copyright 20010 Yannick Stucki (yannickstucki.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yannickstucki.android.musicqueue.data;

import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.MediaStore;

/**
 * A class implementing the unified interface to access all database resources.
 * 
 * @author Yannick Stucki (yannickstucki@gmail.com)
 * 
 */
public class DatabaseFacade implements IDatabase {

  /**
   * The starred songs are stored in the database adapter.
   */
  private DbAdapter dbAdapter;

  /**
   * Information about all the songs is stored in the system's own database
   * accessed through a content resolver.
   */
  private ContentResolver contentResolver;

  /**
   * The database facade works by invoking methods on the objects passed in
   * through the constructor.
   */
  public DatabaseFacade(DbAdapter dbAdapter, ContentResolver contentResolver) {
    this.dbAdapter = dbAdapter;
    this.contentResolver = contentResolver;
  }

  @Override
  public void close() {
    dbAdapter.close();
  }

  @Override
  public Cursor getSongCursor() {
    return contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] {
        MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.ARTIST,
        MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.DURATION,
        MediaStore.Audio.Media.IS_MUSIC, MediaStore.Audio.Media._ID }, null, null, null);
  }

  @Override
  public Cursor getStarredSongCursor() {
    return dbAdapter.getStarredSongCursor();
  }

  @Override
  public IDatabase open() {
    dbAdapter.open();
    return this;
  }
}
